/*
 * @lc app=leetcode.cn id=20 lang=java
 *
 * [20] 有效的括号
 */

 import java.util.Stack;
// @lc code=start
class Solution {
    public boolean isValid(String s) {
        if(s.length() % 2 != 0) return false;
        if(s.length() == 0 || s == "") return true;
        Stack stack = new Stack();  // 创建一个栈

        for(int i=0; i< s.length(); i++) {
            char c = s.charAt(i);
            if(c == '(' || c == '[' || c == '{') {
                stack.push(c);
            } else {
                if(stack.isEmpty()) return false;
                char topChar = (char) stack.pop();
                if(c == ')' && topChar != '(') return false;
                if(c == ']' && topChar != '[') return false;
                if(c == '}' && topChar != '{') return false;
            }
        }
        return stack.isEmpty();

    }
}
// @lc code=end

 